let QQMapWX = require('qqmap-wx-jssdk.min');
let qqmapsdk;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myLatitude: 0.0,
    myLongitude: 0.0,
    list:[],
    curid:'',
    cur:'',
    text:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let page = this


    /**
     * 实例化API核心类(详情见申请key)：http://lbs.qq.com/console/mykey.html
     */
    qqmapsdk = new QQMapWX({ 
      key: 'NXZBZ-PLNCR-WDNWR-W5EPB-LGCHQ-FOF3B' //XXXX-XXXX-XXXX-XXXX
    });

    /**
     * 调用内部获取位置，默认为wsg84,精确为gcj02
     */
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        console.log(res.latitude, res.longitude);
        page.setData({
          myLatitude: res.latitude,
          myLongitude: res.longitude
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getLngLat()
  },


  /**
   * 获取中间点的经纬度，并mark出来
   */
  getLngLat() {
    let page = this;
    page.mapCtx = wx.createMapContext("map");
    page.mapCtx.getCenterLocation({
      success: function(res) {
        console.log("移动时候的信息",res);
        page.setData({
          markers: [{
            id: 0,
            iconPath: "location.png",
            longitude: res.longitude,
            latitude: res.latitude,
            width: 60,
            height: 60,
            callout :{
              content: page.data.text, 
              color: '#000000',
              fontSize: '32px',      
              anchorX: '30px', 
              anchorY: '30px', 
              display: 'ALWAYS'
            } ,
             
          }]
        }) 
        page.getPoiList(res.longitude, res.latitude) 
      }
    })
  },

  /**
   * 视野发生变化时触发：见页面bindregionchange事件
   */
  regionchange(e) {
    e.type == 'end' ? this.getLngLat() : this.getLngLat()
  }, 
  /**
   * 详情见官方API配置:http://lbs.qq.com/qqmap_wx_jssdk/method-reverseGeocoder.html
   */
  getPoiList(longitude, latitude) {
    let page = this
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      get_poi: 1,
      poi_options: 'address_format=short;policy=2;radius=3000;page_size=10;page_index=1',
      success: function(res) { 
        page.setData({
          text: res.result.pois[0].title,
          list: res.result.pois,
          // curid:res.result.pois[0].id,
        });
        // page.setMap(res.result.pois,res.result.pois[0].id)
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {

      }
    });
  },
  setMap(list,curid){ 
    app.globalData.map= list.filter(e=>e.id== curid)[0]
    console.log(app.globalData.map);
  },
  radioChange(e) { 
    this.setData({ curid:e.detail.value })
    this.setMap(this.data.list,e.detail.value)
  }
})